from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from libs.browser.chrome import Chrome


class BaseDriver:
    #设置参数browser=chrome
    def __init__(self, browser='chrome'):
        # 启动浏览器
        browser = browser.lower()
        # 调取浏览器为chrome或者 ch都可以启动该浏览器
        if browser == 'chrome' or browser == 'ch':
            # 获取浏览器chrome中的的url地址
            self.driver = Chrome().get_driver()
            # 设置最大化浏览器
        self.driver.maximize_window()

    # 设置参数locator
    def find_singular(self,locator):
        """将获取的locator转换为web元素,查找单个元素"""
        # isinstance() 函数来判断一个对象是否是一个已知的类型,判断该元素是不是元组
        if isinstance(locator,tuple):
            try:
                param = self.locator_validation(locator)
                # 显示等待 WebDriverWait ,调用该方法提供的驱动程序作为参数，直到返回值为True
                # 浏览器获取元素之后,等待10秒,下面是固定写法
                element = WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located(param)  #param是解析元素,转换成web元素element
                )
                # 找到元素之后返回元素,到此查找元素结束,反之抛出异常
                return element
            except Exception as e:
                msg = f"""单个查找,未找到元素{locator}"""
                return msg
        elif isinstance(locator, WebElement):
            return locator
        else:
            msg = f"""locator传递的参数不正确,需要元组或者webElement类型的参数:{locator}"""
            raise ValueError(f"locator传递的参数不正确,需要元组或者webElement类型的参数:{locator}")

    def find_plural(self,locator) -> list:
        """查找多个元素"""
        try:
            param = self.locator_validation(locator)
            element = WebDriverWait(self.driver, 10).until(
                #lambda是匿名函数
                lambda x:x.find_elements(*param))
            return element
        except Exception as e:
            msg = f"查找多个,未找到的元素{locator}"
            raise Exception()

    @staticmethod
    def locator_validation(locator):
        if not isinstance(locator, tuple):
            raise Exception("输入的格式必须时(by,value)格式")
        selector,value = locator
        if selector == 'ID' or selector == "id":
            param = (By.ID, value)
        elif selector == 'XPATH' or selector == 'xpath':
            param = (By.XPATH, value)
        elif selector == 'LINK_TEXT' or selector == 'link_text':
            param = (By.LINK_TEXT,value)
        elif selector == 'PARTIAL_LINK_TEXT' or selector == 'partial_link_text':
            param = (By.PARTIAL_LINK_TEXT,value)
        elif selector == 'NAME' or selector == 'name':
            param =(By.NAME,value)
        elif selector == 'TAG_NAME' or selector == 'tag_name':
            param =(By.TAG_NAME,value)
        elif selector == 'CLASS_NAME' or selector == 'class_name':
            param =(By.CLASS_NAME,value)
        elif selector == 'CSS_SELECTOR' or selector == 'css_selector':
            param =(By.CSS_SELECTOR,value)
        else:
            raise ValueError(f"输入的格式不对,必须是以下格式(id,123")
        return param
